<template>
	<view class="online">
		<view class="chat-container">
			<view v-for="(chat, index) in chatList" :key="index"
				:class="['chat-item', { 'other-side': chat.type === 'admin' }]">
				<view class="avatar-container">
					<image :src="chat.type === 'admin' ? adminAvatar : userAvatar" class="avatar"></image>
				</view>
				<view class="chat-content-container">
					<view class="username" :class="{ 'me': chat.type !== 'admin' }">
						<view v-if="chat.type === 'admin'">管理员</view>
						<view v-if="chat.type === 'user'">{{username}}</view>
					</view>
					<view class="message-wrapper" :class="{ 'me': chat.type !== 'admin' }">
						<view class="message">{{ chat.msg }}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="input-container">
			<input v-model="newMessage" class="input" placeholder="输入消息..." />
			<button @click="send" class="send-button">发送</button>
		</view>
	</view>
</template>

<script>
	import imgSrc from "@/static/logo.png"
	export default {
		data() {
			return {
				adminAvatar: imgSrc,
				userAvatar: "",
				chatList: [],
				newMessage: '',
				socket: null,
				username: '客户名称',
				webtar:''
			};
		},
		mounted() {
		},
		onLoad(options) {
		   this.userAvatar = options.userpic;
		   this.username = options.nickname;
		   this.webtar = options.username;
			this.crcx();
		},
		beforeDestroy() {
			// 在组件销毁前关闭 WebSocket 连接
			if (this.socket) {
				this.socket.close();
				this.socket = null;
			}
		},
		methods: {
			crcx() {
				let This = this;
				// 初始化 WebSocket 连接
				this.socket = uni.connectSocket({
					url: 'ws://192.168.31.31:30001/api/websocket/onlineService/0/'+this.webtar,
					// url: 'ws://81.68.130.79:20001/api/websocket/onlineService/0/1212',
					complete: () => {}
				});
				uni.onSocketOpen(function(res) {
					console.log('WebSocket连接已打开！');
				});
				uni.onSocketMessage(function(res) {
					const message = res.data;
					console.log('收到消息:', message);
					// 假设消息格式为 JSON，并且包含 type 和 content 字段
					try {
						let data = JSON.parse(message);
						console.log(data.type);
						if (data.type != "content") {
							uni.showToast({
								title: data.msg
							});
						} else {
							data.type = "user";
							This.chatList.push({
						type: 'user',
						msg: data.msg
					});
						}
					} catch (error) {
						console.error('解析消息失败:', error);
					}
				});
				// 连接关闭时触发
				uni.onSocketClose(function (res) {
				  console.log('WebSocket 已关闭！');
				});
			},
			send() {
				if (this.newMessage.trim() !== '') {
					this.chatList.push({
						type: 'admin',
						msg: this.newMessage
					});
					let obj = {
						type: 'content',
						msg: this.newMessage
					};
					this.socket.send({data:JSON.stringify(obj)});
					this.newMessage = '';
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.online {
		width: 100%;
		height: 100%;
		position: relative;
	}

	.chat-container {
		display: flex;
		flex-direction: column;
		padding: 10px;
		background-color: #f5f5f5;
		height: calc(100vh - 130px);
		/* Adjusted height to account for input container */
		overflow-y: auto;
	}

	.chat-item {
		display: flex;
		margin-bottom: 10px;
		align-items: flex-start;
	}

	.other-side {
		flex-direction: row-reverse;
	}

	.avatar-container {
		width: 40px;
		height: 40px;
		margin: 0 10px;
	}

	.avatar {
		width: 100%;
		height: 100%;
		border-radius: 50%;
	}

	.chat-content-container {
		max-width: 70%;
		display: flex;
		flex-direction: column;
	}

	.message-wrapper {
		background-color: #ffffff;
		padding: 10px;
		border-radius: 5px;
		box-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
		margin-top: 5px;
	}

	.message-wrapper.me {
		background-color: #dcf8c6;
	}

	.username {
		font-size: 12px;
		color: #888888;
		margin-bottom: 5px;
		text-align: right;
	}

	.username.me {
		text-align: left;
	}

	.message {
		word-wrap: break-word;
		font-size: 14px;
	}

	.input-container {
		display: flex;
		height: 40px;
		align-items: center;
		position: fixed;
		bottom: 0;
		width: 90%;
		margin-left: 5%;
	}

	.input {
		flex: 1;
		padding-right: 2px;
		height: 20px;
	}

	.send-button {
		background-color: #07c160;
		color: #ffffff;
		border: none;
		width: 80px;
		height: 30px;
		font-size: 14px;
		line-height: 30px;
		border-radius: 10px;
		cursor: pointer;
	}
</style>